<template>
  <div style="min-height: 76vh">
    <el-form @keyup.enter.native="getData()" :inline="true">
      <el-form-item>
        <select-credit-goods
          class="form-control-ml"
          clearable
          filterable
          default-first-option
          v-model="searchDamId"
          @select="name => (params.project_type_name = name)"
          placeholder="产业包名称"
        ></select-credit-goods>
      </el-form-item>
      <!-- <el-form-item>
        <select-address v-model="params.address_code"
                        placeholder="地区选择"
                        auto-top
                        clearable
                        @change="getData()"></select-address>
      </el-form-item> -->
      <el-form-item>
        <el-button @click="getData()">查询</el-button>
      </el-form-item>
    </el-form>

    <c-table
      border
      v-loading="loading"
      :stripe="false"
      :columns="tableColumns"
      :data="tableData"
    ></c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="tableCount"
        @current-change="getData"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
// import SelectAddress from '@/components/select/SelectAddress'
import SelectCreditGoods from '@/gov/components/select/SelectCreditGoods'

export default {
  components: {
    // SelectAddress,
    SelectCreditGoods,
  },
  props: {},
  data() {
    return {
      searchDamId: '',
      params: {
        index: 1,
        size: 20,
        dam_name: '',
        address_code: '',
        'project_type.eq': 3,
      },
      loading: false,
      tableName: 'customer_related',
      tableCount: 0,
      tableData: [],
      tableColumns: [
        { attrs: { label: '产业包名称', prop: 'project_type_name' } },
        // { attrs: { label: '地址', prop: '' } },
        { attrs: { label: '申请项目', prop: 'p' } },
        { attrs: { label: '申请金额', prop: 'p1' } },
        { attrs: { label: '放款金额', prop: 'p2' }, process: v => v || 0 },
        { attrs: { label: '在保余额', prop: 'p3' }, process: v => v || 0 },
      ],
    }
  },
  computed: {
    user() {
      return this.$store.getters.user
    },
  },
  watch: {},
  created() {
    this.params.address_code = this.user.queryAddressCode
    this.getData()
  },
  mounted() {},
  methods: {
    async getData(index = 1) {
      this.params.index = index
      this.loading = true
      this.params.fields = ['project_type_name']
      this.params.groupFields = 'project_type_id'
      this.params.groupFunc = `count(1),sum(${this.tableName}.apply_funds),sum(${this.tableName}.reply_funds),sum(${this.tableName}.left_funds)`
      const [res] = await this.$utils.promise(
        this.$api.getData(this.$serviceName, this.tableName, this.params)
      )
      this.loading = false
      this.getCount()
      const data = (res && res.data) || {}
      this.tableData = data.data || []
      this.tableCount = data.totalCount || 0
    },
    getCount() {
      const { index, size, ...params } = this.params
      params.groupFunc = `count(1),sum(${this.tableName}.apply_funds),sum(${this.tableName}.reply_funds),sum(${this.tableName}.left_funds)`
      params.groupFields = 'project_type'
      this.$api.getData(this.$serviceName, this.tableName, params).then(res => {
        const sum = (res && res.data && res.data[0]) || {}
        this.$set(this.tableColumns[1].attrs, 'label', `申请项目 (${sum.p || 0})`)
        this.$set(this.tableColumns[2].attrs, 'label', `申请金额 (${sum.p1 || 0} 万元)`)
        this.$set(this.tableColumns[3].attrs, 'label', `放款金额 (${sum.p2 || 0} 万元)`)
        this.$set(this.tableColumns[4].attrs, 'label', `在保余额 (${sum.p3 || 0} 万元)`)
      })
    },
  },
}
</script>
<style lang="scss" scoped></style>
